﻿using ChartManager.Controllers;
using ChartManager.CustomControls;
using ChartManager.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChartManager
{
    /// <summary>
    /// 选择歌曲模式
    /// </summary>
    public enum SelectMode
    {
        /// <summary>
        /// 上周
        /// </summary>
        LastWeek,
        /// <summary>
        /// 新歌
        /// </summary>
        New,
        /// <summary>
        /// 所有
        /// </summary>
        All,
    }

    public partial class Form2 : Form
    {
        public Form2(EFContext context, SelectMode mode)
        {
            InitializeComponent();
            Text = "选择歌曲";
            InitialTable(context, mode);
        }

        private void InitialTable(EFContext context, SelectMode mode)
        {
            if (mode == SelectMode.LastWeek)
            {
                try
                {
                    // 表头
                    tableLayoutPanel1.Controls.Add(new HeaderLabel("Rank"), 0, 0);
                    tableLayoutPanel1.Controls.Add(new HeaderLabel("歌曲"), 1, 0);

                    // 获取上周上榜歌曲并填入表中
                    var lastweek = ChartController.GetLastWeek(context);
                    foreach (var row in lastweek.Rows)
                    {
                        tableLayoutPanel1.Controls.Add(new HeaderLabel(row.Rank.ToString()), 0, row.Rank);
                        tableLayoutPanel1.Controls.Add(new HeaderLabel($"{row.Song.SingersAsString} - {row.Song.Name}"), 1, row.Rank);
                        var button = new TextButton("选择此项");
                        button.Click += (object sender, EventArgs e) =>
                        {
                            Result = row.Song;
                            DialogResult = DialogResult.OK;
                        };
                        tableLayoutPanel1.Controls.Add(button, 2, row.Rank);
                    }
                }
                // 获取上周榜单时发生异常，无法找到上周榜单
                catch (IndexOutOfRangeException e)
                {
                    MessageBox.Show(e.Message);
                    DialogResult = DialogResult.Abort;
                }
            }
            else if (mode == SelectMode.New)
            {
                // 表头
                tableLayoutPanel1.Controls.Add(new HeaderLabel("歌手"), 0, 0);
                tableLayoutPanel1.Controls.Add(new HeaderLabel("歌曲"), 1, 0);

                // 获取新歌曲并填入表中
                var newSongs = SongController.GetNewSongs(context);
                // 没有新歌曲时弹窗并关闭本窗体
                if (newSongs.Count() < 1)
                {
                    MessageBox.Show("没有新歌曲");
                    Close();
                }
                for (int i = 0; i < newSongs.Count(); i++)
                {
                    tableLayoutPanel1.Controls.Add(new HeaderLabel(newSongs.ElementAt(i).SingersAsString), 0, i + 1);
                    tableLayoutPanel1.Controls.Add(new HeaderLabel(newSongs.ElementAt(i).Name), 1, i + 1);
                    var button = new TextButton("选择此项");
                    button.Click += (object sender, EventArgs e) =>
                    {
                        Result = newSongs.ElementAt(tableLayoutPanel1.GetRow(button) - 1);
                        DialogResult = DialogResult.OK;
                    };
                    tableLayoutPanel1.Controls.Add(button, 2, i + 1);
                }
            }
            else if (mode == SelectMode.All)
            {
                // 表头
                tableLayoutPanel1.Controls.Add(new HeaderLabel("歌手"), 0, 0);
                tableLayoutPanel1.Controls.Add(new HeaderLabel("歌曲"), 1, 0);

                // 获取所有歌曲并填入表中
                var allSongs = SongController.GetSongs(context, false);
                // 没有任何歌曲时弹窗并关闭本窗体
                if (allSongs.Count() < 1)
                {
                    MessageBox.Show("没有歌曲");
                    Close();
                }
                for (int i = 0; i < allSongs.Count(); i++)
                {
                    tableLayoutPanel1.Controls.Add(new HeaderLabel(allSongs.ElementAt(i).SingersAsString), 0, i + 1);
                    tableLayoutPanel1.Controls.Add(new HeaderLabel(allSongs.ElementAt(i).Name), 1, i + 1);
                    var button = new TextButton("选择此项");
                    button.Click += (object sender, EventArgs e) =>
                    {
                        Result = allSongs.ElementAt(tableLayoutPanel1.GetRow(button) - 1);
                        DialogResult = DialogResult.OK;
                    };
                    tableLayoutPanel1.Controls.Add(button, 2, i + 1);
                }
            }
        }

        /// <summary>
        /// 选择返回结果
        /// </summary>
        public Song Result { get; private set; }
    }
}
